package org.jsets.fastboot.oss.controller;

import org.jsets.fastboot.model.DataResp;
import org.jsets.fastboot.model.ListResp;
import org.jsets.fastboot.oss.model.dto.OutsideQuery;
import org.jsets.fastboot.oss.model.entity.Outside;
import org.jsets.fastboot.oss.service.IOutsideService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@RestController
@RequestMapping("/oss/outsides")
@Api(tags = "外链")
public class OutsideController {

	@Autowired
	private IOutsideService service;

	@PostMapping(produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE)
	@ApiOperation("创建外链")
	public DataResp<String> insert(@RequestBody @Validated Outside entity) {
		DataResp<String> respond = DataResp.ok();
		respond.setData(this.service.insertOutside(entity));
		return respond;
	}
	
	@GetMapping(produces = MediaType.APPLICATION_JSON_VALUE)
	protected ListResp<Outside> list(
			@RequestParam(name = "page_no", required = false) Integer pageNo,
			@RequestParam(name = "page_size", required = false) Integer pageSize,
			@RequestParam(name = "file_name", required = false) String fileName,
			@RequestParam(name = "bucket_name", required = false) String bucketName,
			@RequestParam(name = "status", required = false) Integer status) {
		OutsideQuery criteria = new OutsideQuery()
				.setCurrent(pageNo)
				.setSize(pageSize)
				.setFileName(fileName)
				.setBucketName(bucketName)
				.setStatus(status);
		
		ListResp<Outside> respond = ListResp.ok();
		Page<Outside> result = this.service.selectPage(criteria);
		respond.setData(result.getRecords());
		respond.setTotalCount(result.getTotal());
		return respond;
	}

}